<?php
class orders
{

	public $db;

	public $step = 1;
	
	public $id;
	public $profile_id;
	public $bonus_id;
	public $date;
	public $payment;
	public $status;
	
	public $details;
	public $products = null;

	
	function __construct()
	{
		$this->db = new database();
	}
	
	function toDB() {
		$this->id = $this->db->insert("INSERT INTO `order` VALUES(NULL,0,'".$this->profile_id."', '".time()."', '".$this->payment."')");
		
		foreach($this->products as $product_id => $quantity) {
			$this->db->insert("INSERT INTO `detail` VALUES('".$product_id."', '".$this->id."', '".$this->products[$product_id]['quantity']."')");
		}
	}
	
	function fromArray($row)
	{
		// Informazioni 
		$this->id = $row['ID'];
		$this->profile_id = $row['profile_ID'];
		$this->bonus_id = $row['bonus_ID'];
		$this->date = $row['date'];
		$this->payment = $row['payment'];
		
		$this->loadStatus();
		$this->loadDetails();
	}
	
	function loadStatus()
	{
		$status_q = $this->db->select("SELECT * FROM `statuslog` WHERE `statuslog`.`order_ID` = '" . $this->id ."' ORDER BY `statuslog`.`ID` DESC");
		
		while($status = mysql_fetch_array($status_q)) {
			$key = count($this->status);

			$this->status[$key]['status'] = $status['status'];
			$this->status[$key]['date'] = $status['date'];
			$this->status[$key]['comment'] = $status['comment'];
		}
	}
	
	function loadDetails()
	{
		$products = $this->db->select("SELECT `detail`.`quantity`, `product`.* FROM `detail`, `product` WHERE `detail`.`product_ID` = `product`.`ID` AND `detail`.`order_ID` = '" . $this->id ."'");
		
		while($product = mysql_fetch_array($products)) {
			$this->addProduct($product['ID'], $product['name'], $product['description'], $product['price'], $product['quantity']);
		}
	}

	function getOrders() 
	{
		$result = $this->db->select("SELECT * FROM `detail` ORDER BY `order_ID`");
		$i = 0;
		while($row = mysql_fetch_array($result)) {
			$this->details[$i]['product_ID'] = $row['product_ID'];
			$this->details[$i]['order_ID'] = $row['order_ID'];
			$this->details[$i]['quantity'] = $row['quantity'];
			
			$i++;
		}
		
		return $this->details;
	}
	
	function getProduct($id) 
	{
		$result = $this->db->select("SELECT * FROM `product` WHERE `ID` = '" . $id . "'");
	
		while($row = mysql_fetch_array($result)) {
			$this->product['name'] = $row['name'];
			$this->product['price'] = $row['price'];
		}
		
		return $this->product;
	}
	
	function getProducts()
	{
		if(count($this->products) > 0) {
			return $this->products;
		} else {
			return false;
		}
	}
	
	function getLastStatusDate()
	{
		if($this->status[0]['status'] != null) {
			return date("d.m.Y H:i", $this->status[0]['date']);
		}
	}
	
	function getLastStatus()
	{
		if($this->status[0]['status'] == null) {
			return 'Problema riscontrato';
		} else {
			return $this->status[0]['status'];
		}
	}
	
	function getLastStatusFormat()
	{
		if($this->status[0]['status'] == null) {
			return 'Problema riscontrato';
		} else {
			return $this->formatStatus($this->status[0]['status']);
		}
	}
	
	function formatStatus($status)
	{
		switch($status) {
			case -3:
				return 'Ordine annullato da PizzaWay';
				break;
			case -2:
				return 'Annullato dall\'utente';
				break;
			case -1:
				return 'In attesa di conferma';
				break;
			case 0:
				return 'In fase di preparazione';
				break;
			case 1:
				return 'Pronto per la consegna';
				break;
			case 2:
				return 'Ordine in viaggio';
				break;
			case 3:
				return 'Ordine consegnato';
				break;
		}
	}
	
	function addProduct($id, $name, $description, $price, $quantity)
	{
		$this->products[$id]['quantity'] = $quantity;
		$this->products[$id]['name'] = $name;
		$this->products[$id]['description'] = $description;
		$this->products[$id]['price'] = $price;
		
		$this->save();
	}
	
	function getSumPrice()
	{
		foreach($this->products as $product) {
			$price = $price+($product['quantity']*$product['price']);
		}
		
		return $price;
	}
	
	function save() 
	{
		$_SESSION['order'] = serialize($this);
	}
	
	function getStep()
	{
		return $this->step;
	}
	
	function setPayment($payment)
	{
		switch($payment) {
			case "bill":
				$this->payment = 1;
				return true;
				break;
			case "paypal":
				$this->payment = 2;
				return true;
				break;
			case "bonus":
				$this->payment = 0;
				return true;
				break;
			default:
				return false;
				break;
		}
	}
	
	function getPayment()
	{
		switch($this->payment) {
			case 1:
				return "Pagamento alla consegna";
				break;
			case 2:
				return "Pagamento tramite PayPal";
				break;
			case 0:
				return "Pagamento utilizzando bonus";
				break;
		}
	}
	

}

?>